<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="合同序号" prop="contId">
      <el-input v-model="dataForm.contId" placeholder="合同序号"></el-input>
    </el-form-item>
    <el-form-item label="物资编码" prop="materialCode">
      <el-input v-model="dataForm.materialCode" placeholder="物资编码"></el-input>
    </el-form-item>
    <el-form-item label="物资名称" prop="materialName">
      <el-input v-model="dataForm.materialName" placeholder="物资名称"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="amount">
      <el-input v-model="dataForm.amount" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="单价(元/含税)" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="单价(元/含税)"></el-input>
    </el-form-item>
    <el-form-item label="计量单位" prop="measureUnit">
      <el-input v-model="dataForm.measureUnit" placeholder="计量单位"></el-input>
    </el-form-item>
    <el-form-item label="总价(元/含税)" prop="sumPrice">
      <el-input v-model="dataForm.sumPrice" placeholder="总价(元/含税)"></el-input>
    </el-form-item>
    <el-form-item label="运杂费(元/含税)" prop="mixPrice">
      <el-input v-model="dataForm.mixPrice" placeholder="运杂费(元/含税)"></el-input>
    </el-form-item>
    <el-form-item label="其他(元/含税)" prop="otherPrice">
      <el-input v-model="dataForm.otherPrice" placeholder="其他(元/含税)"></el-input>
    </el-form-item>
    <el-form-item label="总计(元/含税)" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="总计(元/含税)"></el-input>
    </el-form-item>
    <el-form-item label="到货期" prop="endDate">
      <el-input v-model="dataForm.endDate" placeholder="到货期"></el-input>
    </el-form-item>
    <el-form-item label="交(提)货地点" prop="address">
      <el-input v-model="dataForm.address" placeholder="交(提)货地点"></el-input>
    </el-form-item>
    <el-form-item label="运输方式" prop="mode">
      <el-input v-model="dataForm.mode" placeholder="运输方式"></el-input>
    </el-form-item>
    <el-form-item label="包装要求" prop="wrapRequire">
      <el-input v-model="dataForm.wrapRequire" placeholder="包装要求"></el-input>
    </el-form-item>
    <el-form-item label="制造厂" prop="factory">
      <el-input v-model="dataForm.factory" placeholder="制造厂"></el-input>
    </el-form-item>
    <el-form-item label="其他要求" prop="otherRequire">
      <el-input v-model="dataForm.otherRequire" placeholder="其他要求"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          contId: '',
          materialCode: '',
          materialName: '',
          amount: '',
          unitPrice: '',
          measureUnit: '',
          sumPrice: '',
          mixPrice: '',
          otherPrice: '',
          totalPrice: '',
          endDate: '',
          address: '',
          mode: '',
          wrapRequire: '',
          factory: '',
          otherRequire: ''
        },
        dataRule: {
          contId: [
            { required: true, message: '合同序号不能为空', trigger: 'blur' }
          ],
          materialCode: [
            { required: true, message: '物资编码不能为空', trigger: 'blur' }
          ],
          materialName: [
            { required: true, message: '物资名称不能为空', trigger: 'blur' }
          ],
          amount: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '单价(元/含税)不能为空', trigger: 'blur' }
          ],
          measureUnit: [
            { required: true, message: '计量单位不能为空', trigger: 'blur' }
          ],
          sumPrice: [
            { required: true, message: '总价(元/含税)不能为空', trigger: 'blur' }
          ],
          mixPrice: [
            { required: true, message: '运杂费(元/含税)不能为空', trigger: 'blur' }
          ],
          otherPrice: [
            { required: true, message: '其他(元/含税)不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '总计(元/含税)不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '到货期不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '交(提)货地点不能为空', trigger: 'blur' }
          ],
          mode: [
            { required: true, message: '运输方式不能为空', trigger: 'blur' }
          ],
          wrapRequire: [
            { required: true, message: '包装要求不能为空', trigger: 'blur' }
          ],
          factory: [
            { required: true, message: '制造厂不能为空', trigger: 'blur' }
          ],
          otherRequire: [
            { required: true, message: '其他要求不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/contractdetail/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.contId = data.contractDetail.contId
                this.dataForm.materialCode = data.contractDetail.materialCode
                this.dataForm.materialName = data.contractDetail.materialName
                this.dataForm.amount = data.contractDetail.amount
                this.dataForm.unitPrice = data.contractDetail.unitPrice
                this.dataForm.measureUnit = data.contractDetail.measureUnit
                this.dataForm.sumPrice = data.contractDetail.sumPrice
                this.dataForm.mixPrice = data.contractDetail.mixPrice
                this.dataForm.otherPrice = data.contractDetail.otherPrice
                this.dataForm.totalPrice = data.contractDetail.totalPrice
                this.dataForm.endDate = data.contractDetail.endDate
                this.dataForm.address = data.contractDetail.address
                this.dataForm.mode = data.contractDetail.mode
                this.dataForm.wrapRequire = data.contractDetail.wrapRequire
                this.dataForm.factory = data.contractDetail.factory
                this.dataForm.otherRequire = data.contractDetail.otherRequire
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/contractdetail/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'contId': this.dataForm.contId,
                'materialCode': this.dataForm.materialCode,
                'materialName': this.dataForm.materialName,
                'amount': this.dataForm.amount,
                'unitPrice': this.dataForm.unitPrice,
                'measureUnit': this.dataForm.measureUnit,
                'sumPrice': this.dataForm.sumPrice,
                'mixPrice': this.dataForm.mixPrice,
                'otherPrice': this.dataForm.otherPrice,
                'totalPrice': this.dataForm.totalPrice,
                'endDate': this.dataForm.endDate,
                'address': this.dataForm.address,
                'mode': this.dataForm.mode,
                'wrapRequire': this.dataForm.wrapRequire,
                'factory': this.dataForm.factory,
                'otherRequire': this.dataForm.otherRequire
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
